package 设计模式.创建型.工厂模式.config;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Component;
import 设计模式.创建型.工厂模式.sc.ScheduleClass;

import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author lchenglong
 * @date 2024/1/26
 */
@Component
public class ScheduleClassBeanFactory {
    private Map<String, ScheduleClass<String>> scheduleClassMap;

    @Autowired
    private List<ScheduleClass> shapeList;

    @Autowired
    private ApplicationContext applicationContext;

    @Autowired
    public void setScheduleClassMap(Map<String, ScheduleClass<String>> scheduleClassMap){
        // 获取 Spring 容器中，所有 S 类型的 Bean
        Collection<ScheduleClass> scheduleClassList = applicationContext.getBeansOfType(ScheduleClass.class).values();

        scheduleClassMap = new HashMap<>(scheduleClassList.size());

        // 将所有 S 类型的 Bean 放入到 strategyMap 中
        for (final ScheduleClass<String> scheduleClass : scheduleClassList) {
            String id = scheduleClass.getId();
            scheduleClassMap.put(id, scheduleClass);
        }
        this.scheduleClassMap = scheduleClassMap;
    }

    public ScheduleClass getSchedule(String shapeType) {
        ScheduleClass scheduleClassBean = scheduleClassMap.get(shapeType);
        System.out.println(scheduleClassBean);
        return scheduleClassBean;
    }

}
